#!/usr/bin/python
# -*- coding:UTF-8 -*-
# @Time    : 2019/1/10 8:15
# @Author  : 王志鹏
# @Site    : 
# @File    : d2.py
# @Software: PyCharm

"""
内部与外部状态
"""
import abc


class User(object):
    def __init__(self, name):
        self.name = name


class WebSite(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def Use(self, user): pass


# 享元工厂 管理具体的享元对象
class WebsiteFactory(object):
    def __init__(self):
        self.flyweights = {}

    def getWebSiteCategory(self, keyName):
        if not self.flyweights.has_key(keyName):
            self.flyweights[keyName] = ConcreteWebSite(keyName)
        return self.flyweights[keyName]

    def getWebSite(self):
        print "网站分类总数为%s" % len(self.flyweights)


class ConcreteWebSite(WebSite):
    def __init__(self, name):
        self.name = name

    def Use(self, user):
        print "网站分类:%s -- 用户:%s" % (self.name, user.name)


if __name__ == '__main__':
    f = WebsiteFactory()

    fx = f.getWebSiteCategory("产品展示")
    fx.Use(User("小菜"))

    fy = f.getWebSiteCategory("产品展示")
    fy.Use(User("大鸟"))

    fz = f.getWebSiteCategory("博客")
    fz.Use(User("娇娇"))

    fl = f.getWebSiteCategory("博客")
    fl.Use(User("花花"))

    fm = f.getWebSiteCategory("博客")
    fm.Use(User("草草"))

    f.getWebSite()
